package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.servlet.http.HttpSession;
import org.example.pojo.Result;
import org.example.pojo.UserFollows;
import org.example.pojo.Users;
import org.example.pojo.vo.UserVo;
import org.example.service.NotesService;
import org.example.service.UserFollowsService;
import org.example.service.UsersService;
import org.example.utils.Current;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

@RestController
@RequestMapping("user")
public class UserController 
{
	@Autowired
	UsersService usersService;
	
	@Autowired
	NotesService notesService;
	
	@Autowired
	UserFollowsService userFollowsService;
	
	@GetMapping("getProfile")
	public Result getProfile(@RequestParam("uid") Integer interestUid)
	{
		Integer uid = (Integer)Current.get("uid");

		System.out.println(uid);
		Users user = usersService.getById(interestUid);
		

		//统计笔记数量
		QueryWrapper qw = new QueryWrapper();
		qw.eq("user_id",interestUid);
		long count = notesService.count(qw);

		//统计粉丝
		Long interest = userFollowsService.count(qw);
		//统计关注
		QueryWrapper qw2 = new QueryWrapper();
		qw2.eq("following_id",interestUid);
		Long fan = userFollowsService.count(qw2);
		
		
		UserVo userVo = new UserVo();
		userVo.setId(user.getId());
		userVo.setUsername(user.getUsername());
		userVo.setAvatar(user.getAvatar());
		//封装统计数
		UserVo.Stats stats = new UserVo.Stats();
		stats.setNotes(count);
		stats.setFollowers(fan);
		stats.setFollowing(interest);
		
		userVo.setStats(stats);
		QueryWrapper qw3 = new QueryWrapper();
		qw3.eq("user_id",uid);
		qw3.eq("following_id",interestUid);
		boolean b = userFollowsService.count(qw3) == 1;
		userVo.setIsFollowing(b);
		if(!b)	//如果关注过才判断互相关注了吗
		{
			userVo.setIsFollowedBack(false);
		}
		QueryWrapper qw4 = new QueryWrapper();
		qw4.eq("user_id",interestUid);
		qw4.eq("following_id",uid);
		
		userVo.setIsFollowedBack(userFollowsService.count(qw4) == 1);
		
		return Result.ok(userVo);
	}

	/**
	 * 检查是否关注
	 */
	@GetMapping("whetherInterest")
	public Result whetherInterest(HttpSession httpSession,@RequestParam Integer followingId)
	{
		Integer uid = (Integer)httpSession.getAttribute("uid");

		Boolean aBoolean = userFollowsService.whetherInterest(uid, followingId);

		return Result.ok(aBoolean);
	}

	@Autowired
	StringRedisTemplate stringRedisTemplate;
	
	private static final String INTEREST_PREFIX = "user:interest:";
	/**
	 * 关注/取消关注
	 */
	@GetMapping("interestCOC")
	public Result interestCOC(HttpSession httpSession,@RequestParam Integer followingId)
	{
		Integer uid = (Integer)httpSession.getAttribute("uid");
		
		if(uid == followingId)
		{
			return Result.ok("不能关注自己");
		}

		QueryWrapper qw = new QueryWrapper();
		qw.eq("user_id",uid);
		qw.eq("following_id",followingId);
		UserFollows one = userFollowsService.getOne(qw);
		if(one != null)
		{
//			stringRedisTemplate.opsForSet().remove(INTEREST_PREFIX+uid,String.valueOf(followingId));
			stringRedisTemplate.opsForSet().remove(INTEREST_PREFIX+uid,String.valueOf(followingId));

			userFollowsService.remove(qw);
			return Result.ok("取消关注");
		}

		stringRedisTemplate.opsForSet().add(INTEREST_PREFIX+uid, String.valueOf(followingId));
		
				
		UserFollows userFollows = new UserFollows();
		userFollows.setUserId(uid);
		userFollows.setFollowingId(followingId);
		userFollowsService.save(userFollows);
		return Result.ok("关注");
	}
	
	//搜索共同关注的人
	@GetMapping("getCommonInterest")
	public Result getCommonInterest(@RequestParam Integer interestId)
	{
		//自己id
		Integer uid = (Integer)Current.get("uid");
		Set<String> union = stringRedisTemplate.opsForSet().intersect(INTEREST_PREFIX + uid, INTEREST_PREFIX + interestId);

//		new QueryWrapper().eq("")	// u -> u.getUsername()
		Object[] objects = union.stream().map(usersService::getById).map(Users::getUsername).toArray();
		
//		System.out.println(objects);
		
		return Result.ok(objects);
	}
}
